package com.k.demo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.k.demo.entity.Balance;
import com.k.demo.entity.CustomerUser;
import com.k.demo.service.BalanceService;
import com.k.demo.service.GoodOrderDetailService;
import com.k.demo.service.GoodOrderService;
import com.k.demo.service.PointDetailService;
import com.k.demo.service.RedBagRecordService;
import com.k.demo.service.RedBagService;

@CrossOrigin
@RestController
@RequestMapping("/balance")
public class BalanceController {
	
	@Autowired
	private GoodOrderService goodOrderService;
	
	@Autowired
	private GoodOrderDetailService goodOrderDetailService;
	
	@Autowired
	private BalanceService balanceService;
	
	@Autowired
	private PointDetailService pointDetailService;
	
	@Autowired
	private RedBagRecordService redBagRecordService;
	
	@Autowired
	private RedBagService redBagService;
	
	@RequestMapping("/getbalances")
	public Object getBalances(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("balancelist",balanceService.getFromStartToEnd((String)map.get("start_date"), (String)map.get("end_date")) );
		
		return rmap;
	}
	@RequestMapping("/adddayreport")
	public Object addBalance(@RequestBody Balance balance) {
		Map<String,Object> rmap = new HashMap<String, Object>();
		List<Balance> balan = balanceService.getFromStartToEnd(balance.getBalance_date(), balance.getBalance_date());
		if(balan.size()==0) {
			balanceService.addBalance(balance);
			rmap.put("insert",true);
			rmap.put("msg", "日结成功");
			return rmap;
		}else {
			rmap.put("insert", false);
			rmap.put("msg", "该日已日结");
			return rmap;
		}
		
		
	}
	@RequestMapping("/getdayreport")
	public Object getTodayMsg(@RequestParam String date,HttpServletRequest request) {
		
		List<Balance> balance = balanceService.getFromStartToEnd(date, date);
		if(balance.size()==0) {
			Map<String,Object> map = new HashMap<String, Object>();
			CustomerUser user=(CustomerUser) request.getSession().getAttribute("loginUser");
			map.put("admin_name", user.getUser_true_name());
			map.put("balance_cost",goodOrderService.getTotalSalesByDate(date));//总销售额
			map.put("balance_actual_cost", goodOrderService.getActualSalesByDate(date));//实际销售额
			map.put("freight_fee", goodOrderService.getNumberByDate(date)*2);
			map.put("packing_fee", goodOrderService.getNumberByDate(date)*2);
			map.put("red_bag_total",redBagRecordService.getTotalValueByDate(date));
			map.put("point_total", pointDetailService.getTotalPointByDate(date)/10);
			map.put("good_order_num", goodOrderService.getNumberByDate(date));
			map.put("food_sale_num", goodOrderDetailService.getSaleNumberByDate(date));
			
			return map;
		}else {
			return balance.get(0);
		}
		
	}
	
	
}
